﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace LVYAN.Utils
{
    public class WinProcess
    {
        public Process[] GetProcessByName(string Name)
        {
            Process[] processes = Process.GetProcessesByName(Name);
            return processes;
        }
        public bool Exists(string Name)
        {
            Process[] processes = Process.GetProcessesByName(Name);
            if (processes == null) return false;
            return processes.Any();
        }
        public int GetProcessCountByName(string Name)
        {
            Process[] processes = Process.GetProcessesByName(Name);
            return processes.Count();
        }
        public int GetProcessIDByName(string Name)
        {
            Process[] processes = GetProcessByName(Name);
            return processes.FirstOrDefault().Id;
        }
        /// <summary>
        /// 内存
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public long GetProcessIDByCacheCount(string Name)
        {
            var count = GetProcessByName(Name).FirstOrDefault().WorkingSet64 / 1024 / 1024;
            return count;
        }
        public Process Start(string Name, List<string> PathList = default)
        {
            string filePath = AppDomain.CurrentDomain.BaseDirectory + Name;
            if (PathList == null) PathList = new List<string> { filePath };
            PathList.Add(filePath);
            var StartPath = string.Empty;
            foreach (var item in PathList)
            {
                var TeamStartPath = Path.GetDirectoryName(item);
                if (Directory.Exists(TeamStartPath) && File.Exists(item))
                {
                    StartPath = item;
                    break;
                }
            }
            if (string.IsNullOrWhiteSpace(StartPath)) throw new Exception("请把应用安装以下任意位置：" + string.Join(",", PathList)) { };
            return Process.Start(StartPath);
        }
        public bool KillALL(string Name)
        {
            Process[] targetProcesses = GetProcessByName(Name);
            foreach (Process process in targetProcesses)
            {
                Console.WriteLine($"尝试结束进程: {process.ProcessName} (ID: {process.Id})");
                process.Kill(); // 强制终止进程
                process.WaitForExit(2000); // 等待进程结束，最多等待2秒
                return process.HasExited;
            }
            return true;
        }
        public bool Kill(string Name)
        {
            var process = GetProcessByName(Name).FirstOrDefault();
            if (process != null)
            {
                Console.WriteLine($"尝试结束进程: {process.ProcessName} (ID: {process.Id})");
                process.Kill(); // 强制终止进程
                process.WaitForExit(2000); // 等待进程结束，最多等待2秒
                return process.HasExited;
            }
            return true;
        }
        public void Kill(string Name,int Count)
        {
            var TotalCount = GetProcessCountByName(Name);
            while (TotalCount> Count)
            {
                Kill(Name);
                TotalCount--;
            }
          
        }
    }
}
